import uuid

from redis import Redis
import time

mconn = Redis(host="192.168.35.102", port=6379, db=0, decode_responses=True)
sconn = Redis(host="192.168.35.102", port=6380, db=0, decode_responses=True)


def wait_for_sync(mconn, sconn):
    ident = str(uuid.uuid4())
    mconn.zadd('sync:wait', {ident: time.time()})
    while not sconn.info()['master_link_status'] == 'up':
        master_link_status = sconn.info()['master_link_status']
        time.sleep(.001)
    while not sconn.zscore('sync:wait', ident):
        time.sleep(.001)
    deadline = time.time() + 1.01
    while time.time() < deadline:
        if sconn.info()['aof_pending_bio_fsync'] == 0:
            break
        time.sleep(.001)
    info = sconn.info()
    mconn.zrem('sync:wait', ident)
    mconn.zremrangebyscore('sync:wait', 0, time.time() - 900)


def print_hi(name):
    # 在下面的代码行中使用断点来调试脚本。
    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    wait_for_sync(mconn, sconn)

# 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助
